﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Maps.Controls;
using System.Device.Location;
using Microsoft.Phone.Maps.Services;

namespace Mapping_Demo
{
    public partial class ReverseGeoCode : PhoneApplicationPage
    {
        public ReverseGeoCode()
        {
            InitializeComponent();
            this.Loaded += ReverseGeoCode_Loaded;
            StateCodes.Add("Alabama", "AL");	
            StateCodes.Add("Alaska", "AK");	
            StateCodes.Add("Arizona", "AZ");	
            StateCodes.Add("Arkansas", "AR");	
            StateCodes.Add("California", "CA");	
            StateCodes.Add("Colorado", "CO");	
            StateCodes.Add("Connecticut", "CT");
            StateCodes.Add("Delaware", "DE");	
            StateCodes.Add("District of Columbia", "DC");
            StateCodes.Add("Florida", "FL");	
            StateCodes.Add("Georgia", "GA");	
            StateCodes.Add("Hawaiʻi", "HI");	
            StateCodes.Add("Idaho", "ID");		
            StateCodes.Add("Illinois", "IL");	
            StateCodes.Add("Indiana", "IN");	
            StateCodes.Add("Iowa", "IA");		
            StateCodes.Add("Kansas", "KS");		
            StateCodes.Add("Kentucky", "KY");	
            StateCodes.Add("Louisiana", "LA");	
            StateCodes.Add("Maine", "ME");		
            StateCodes.Add("Maryland", "MD");	
            StateCodes.Add("Massachusetts", "MA");	
            StateCodes.Add("Michigan", "MI");	
            StateCodes.Add("Minnesota", "MN");	
            StateCodes.Add("Mississippi", "MS");
            StateCodes.Add("Missouri", "MO");	
            StateCodes.Add("Montana", "MT");	
            StateCodes.Add("Nebraska", "NE");	
            StateCodes.Add("Nevada", "NV");		
            StateCodes.Add("New Hampshire", "NH");	
            StateCodes.Add("New Jersey", "NJ");		
            StateCodes.Add("New Mexico", "NM");		
            StateCodes.Add("New York", "NY");		
            StateCodes.Add("North Carolina", "NC");	
            StateCodes.Add("North Dakota", "ND");	
            StateCodes.Add("Ohio", "OH");	
            StateCodes.Add("Oklahoma", "OK");
            StateCodes.Add("Oregon", "OR");	
            StateCodes.Add("Pennsylvania", "PA");	
            StateCodes.Add("Rhode Island", "RI");	
            StateCodes.Add("South Carolina", "SC");	
            StateCodes.Add("South Dakota", "SD");	
            StateCodes.Add("Tennessee", "TN");		
            StateCodes.Add("Texas", "TX");			
            StateCodes.Add("Utah", "UT");			
            StateCodes.Add("Vermont", "VT");		
            StateCodes.Add("Virginia", "VA");		
            StateCodes.Add("Washington", "WA");		
            StateCodes.Add("West Virginia", "WV");	
            StateCodes.Add("Wisconsin", "WI");		
            StateCodes.Add("Wyoming", "WY");		
	
        }		

        void ReverseGeoCode_Loaded(object sender, RoutedEventArgs e)
        {
        }

        ReverseGeocodeQuery reverseGeocode;

        private void Map_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Point tapPosition = e.GetPosition((UIElement)sender);
            GeoCoordinate tapLocation = reverseGeoCodeMap.ConvertViewportPointToGeoCoordinate(tapPosition);
            
            MapLayer newML = new MapLayer();
            MapOverlay addressOverlay = new MapOverlay();
            addressOverlay.Content = new DropPin();
            addressOverlay.GeoCoordinate = tapLocation;

            newML.Add(addressOverlay);
            reverseGeoCodeMap.Layers.Add(newML);

            revGeoCordText.Text = "finding location";
            RevGeoCordBar.Visibility = Visibility.Visible;
            RevGeoCordBar.IsIndeterminate = true;

            ReverseGeocodeQuery reverseGeocode = new ReverseGeocodeQuery();
            reverseGeocode.GeoCoordinate = new GeoCoordinate(tapLocation.Latitude, tapLocation.Longitude);
            reverseGeocode.QueryCompleted += reverseGeocode_QueryCompleted;
            reverseGeocode.QueryAsync();
        }

        void reverseGeocode_QueryCompleted(object sender, QueryCompletedEventArgs<IList<MapLocation>> e)
        {
            if (e.Cancelled)
            {
                revGeoCordText.Text = "operation was cancelled";
            }
            else if (e.Error != null)
            {
                revGeoCordText.Text = "Error: " + e.Error.Message;
            }
            else if (e.Result != null)
            {
                if (e.Result.Count > 0)
                {
                    MapAddress geoAddress = e.Result[0].Information.Address;
                    string stateCode = geoAddress.StateCode;
                    if (stateCode == "")
                    {
                        if (StateCodes.ContainsKey(geoAddress.State))
                            stateCode = StateCodes[geoAddress.State];
                        else
                            stateCode = geoAddress.State;
                    }
                    string addressString1 = geoAddress.HouseNumber + " " + geoAddress.Street;
                    string addressString2 = geoAddress.City + ", " + stateCode + " " + geoAddress.PostalCode;
                    string addressString3 = geoAddress.Country;
                    if (addressString1 != " ")
                        addressString1 = addressString1 + "\n";
                    else
                        addressString1 = "";
                    if (addressString2 != ",  ")
                        addressString2 = addressString2 + "\n";
                    else
                        addressString2 = "";
                    revGeoCordText.Text = addressString1 + addressString2 + addressString3;
                }
                else
                {
                    revGeoCordText.Text = "no address found at that location";
                }
            }

            RevGeoCordBar.Visibility = Visibility.Collapsed;
            RevGeoCordBar.IsIndeterminate = false;

        }



        private Dictionary<string, string> StateCodes = new Dictionary<string,string>();
        
    }
}